$(function(){
    //Create menu on the right
    $('#navigation').menu();

    //Handle search.
    $('#search_title').click(function(){
       $('#search_menu').css({display:'block', visibility: 'visible'});

       var $searchControl = {
            position:$('#search_title').position(),
            height: $('#search_title').outerHeight(),
            space: $('#search_title').outerHeight() - $('#search_title').innerHeight() + 1
       };
       $('#search_menu').css({
           top:($searchControl.position.top + $searchControl.height),
           left:$searchControl.position.left + $searchControl.space
       });
    });

    $('li.menu-option').mouseover(function(){
        $(this).css('color', 'white');
    }).mouseleave(function(){
        $(this).css('color', 'black');
    })
    .click(function(){
        var category_search = $(this).text();
        if(category_search.length > 10){
            category_search = category_search.substr(0,5);
        }
        $('#search_category_title').text(category_search);
        $('#search_sex').val($(this).attr('id'));
        $('#search_menu').css({display:'none', visibility: 'hidden'});
    });

    $('#search_menu').mouseleave(function(){
        $('#search_menu').css({display:'none', visibility: 'hidden'});
    });

    //--Sign in dialog.
    $('#dialog_signin').dialog({
        width: 300,
        height: 230,
        modal: true,
        autoOpen: false,
        buttons: {
            'Sign in':function(){
                $('#dialog_signin').dialog('close');
            },
            Cancel: function(){
                $('#dialog_signin').dialog('close');
            }
        }
    });

    //Cart.
    var $cartBox = $('#cart_items');
    var $cartTitleId = $('#list_product_cart');

    $('#list_product_cart').mouseover(function(event){
        event.preventDefault();
        var $cartBoxTitle = {
          position: $cartTitleId.position(),
          space: $cartTitleId.outerHeight() - $cartTitleId.innerHeight()+ 2,
          height: $cartTitleId.height()
        };
        $cartTitleId.css({border: '1px solid #ccc'});
        $cartBox.css({top: $cartBoxTitle.position.top + $cartBoxTitle.height,
            left: $cartBoxTitle.position.left - $cartBoxTitle.space
        });
        $cartBox.show();
    });

    $('#cart_items').mouseleave(function(){
        $cartTitleId.css({border: 'none'});
        $cartBox.hide();
    })

    //Submit form search.
    $('#search_control').submit(function(event){

    });
});